﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;


namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {


            if (this.panelContenedor.Controls.Count > 0)
                this.panelContenedor.Controls.RemoveAt(0);
            //Hijo1 hijo1 = new Hijo1();
            //hijo1.TopLevel = false;
            //hijo1.FormBorderStyle = FormBorderStyle.None;
            //hijo1.Dock = DockStyle.Fill;
            //this.panelContenedor.Controls.Add(hijo1);
            //this.panelContenedor.Tag = hijo1;
            //hijo1.Show();
            //try
            //{
            //    System.Diagnostics.Process.Start("Instalador_32.bat");
            //}
            //catch (SystemException ess) {
            //    label1.Text = ess.Message;
            //}
        }


        private void btn_descarga_Click(object sender, EventArgs e)
        {
            List<String> list = new List<String>();
            //WebClient webClient = new WebClient();
            //webClient.DownloadDataCompleted += new AsyncCompletedEventHandler(Completed);
            //webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
            //webClient.DownloadDataAsync(new Uri("http://bassart.com.ar/dist/"), @carpeta.Text+"/");

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.bassart.com.ar/htdocs/dist/");
            request.Method = WebRequestMethods.Ftp.ListDirectory;

            request.Credentials = new NetworkCredential("bassart.com.ar", "Pp123456B");

            StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());

            string fileName = streamReader.ReadLine();
            while (fileName != null)
            {
                Console.WriteLine(fileName);
                if(fileName!="." && fileName!="..")
                    list.Add(fileName);
                fileName = streamReader.ReadLine();
            }
            request = null;
            streamReader = null;

            foreach(String file in list){                    
                FtpWebRequest request2 = (FtpWebRequest)WebRequest.Create("ftp://ftp.bassart.com.ar/htdocs/dist/" + file);
                request2.Method = WebRequestMethods.Ftp.DownloadFile;
                request2.Credentials = new NetworkCredential("bassart.com.ar", "Pp123456B");
                FtpWebResponse response = (FtpWebResponse)request2.GetResponse();

                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);

                FileStream writeStream = new FileStream( folder.SelectedPath + "/" + file, FileMode.Create);                
                int Length = 2048;
                Byte[] buffer = new Byte[Length];
                int bytesRead = responseStream.Read(buffer, 0, Length);               
                while (bytesRead > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                    bytesRead = responseStream.Read(buffer, 0, Length);
                }                
                writeStream.Close();
                response.Close(); 


                Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
                reader.Close();
                response.Close();
            }

        }

        private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar.Value = e.ProgressPercentage;
        }

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Download completed!");
        }


    }
}
